package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.config.CacheService;
import com.heima.behavior.service.BehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thread.AppThreadLocalUtil;

@Service
@Slf4j
@Transactional
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    /**
     * 点赞和取消赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult isLikes(LikesBehaviorDto dto) {

        // 校验参数
        if (dto == null || dto.getArticleId() == null
                || dto.getType() < 0 || dto.getType() > 2
                || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        // 保存数据
        if (dto.getOperation() == 0) {
            // 点赞
            // 校验Redis中是否存在该数据
            Object obj = cacheService.hGet("LIKE-BEHAVIOR-" + dto.getArticleId().toString(), user.getId().toString());
            if (obj != null) {
                // 存在表示已经点过赞
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已点赞");
            }

            // 不存在则保存当前的数据
            cacheService.hPut("LIKE-BEHAVIOR-" + dto.getArticleId().toString(),user.getId().toString(), JSON.toJSONString(dto));
        }else {
            // 取消点赞，删除当前的数据
            cacheService.hDelete("LIKE-BEHAVIOR-" + dto.getArticleId().toString(),user.getId().toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        // 校验参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        // 保存阅读次数
        String readBehavior = (String) cacheService.hGet("READ-BEHAVIOR-" + dto.getArticleId().toString(), user.getId().toString());
        if(StringUtils.isNotBlank(readBehavior)){
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(readBehavior, ReadBehaviorDto.class);
            readBehaviorDto.setCount(readBehaviorDto.getCount()+1);
            cacheService.hPut("READ-BEHAVIOR-" + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(readBehaviorDto));

        }else {
            // 保存当前key
            cacheService.hPut("READ-BEHAVIOR-" + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikes(UnLikesBehaviorDto dto) {

        // 校验参数
        if (dto == null || dto.getArticleId() == null || dto.getType() < 0 || dto.getType() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        // 保存数据
        if (dto.getType() == 0) {
            // 不喜欢
            Object object = cacheService.hGet("UNLIKE-BEHAVIOR-" + dto.getArticleId().toString(), user.getId().toString());
            if (object != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已经不喜欢");
            }
            cacheService.hPut("UNLIKE-BEHAVIOR-"+dto.getArticleId().toString(),user.getId().toString(),JSON.toJSONString(dto));

        }else {
            // 取消不喜欢
            cacheService.hDelete("UNLIKE-BEHAVIOR-" + dto.getArticleId().toString(),user.getId().toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
